package com.shixun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shixun.domain.Checkdorm;
import com.shixun.dto.CheckdormDto;
import com.shixun.service.CheckdormService;
import com.shixun.mapper.CheckdormMapper;
import com.shixun.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
* @author ASUS
* @description 针对表【checkdorm】的数据库操作Service实现
* @createDate 2023-06-14 10:25:13
*/
@Service
public class CheckdormServiceImpl extends ServiceImpl<CheckdormMapper, Checkdorm>implements CheckdormService{

    @Autowired
    CheckdormMapper checkdormMapper;
    @Override
    public R checkDormPage(int pageSize, int pageNum,
                           String sname, LocalDateTime beginTime,LocalDateTime endTime, String checkState) {

        IPage<CheckdormDto> checkdormDtoIPage=new Page<>(pageNum,pageSize);
        IPage<CheckdormDto> checkdormDtoIPage1 = checkdormMapper.CheckdormPage(checkdormDtoIPage,sname,
                                                                                beginTime,endTime,checkState);
        return new R(200,"",checkdormDtoIPage1);
    }

    @Override
    public Integer checkDay(Checkdorm checkdorm) {
        //当天日期（哪一天）
        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
//        DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//        String time1=LocalDateTime.now().format(formatter);
        LambdaQueryWrapper<Checkdorm> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Checkdorm::getSid,checkdorm.getSid());
        queryWrapper.like(Checkdorm::getCheckTime,format);
        Integer integer = checkdormMapper.selectCount(queryWrapper);
        return integer;
    }

    /**
     * 查询没请假的学生和他所在宿舍
     * @param pageSize
     * @param pageNum
     * @return
     */
    @Override
    public R checkStudent(int pageSize, int pageNum) {
        Page<CheckdormDto> page=new Page<>(pageNum,pageSize);

        IPage<CheckdormDto> checkDormDtoIPage = checkdormMapper.checkStudent(page, LocalDateTime.now());
        return new R(200,"",checkDormDtoIPage);
    }
}




